﻿<cfsilent>
	<cfscript>
	
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		sessionAdvice = getProperty("serviceFactory").getBean("sessionAdvice");
		datetimeAdvice = getProperty("serviceFactory").getBean("datetimeAdvice");
		
		sequenceQuestionVote = getProperty("serviceFactory").getBean("sequenceQuestionVote");
		
		quotaId = event.getArg("QUOTA");
		answer = event.getArg("OPT");
		
		secureTicket = event.getArg("ST");
		
		studentId = sessionAdvice.getAutherUserID();
		
		postSecure = false;

		/* 回执信息 */
		response = structNew();
		structInsert(response, "success", false, true);
		
		/* 首先做安全检查 */
		try {
			stringOrignal = decrypt(secureTicket, studentId, "PBEWithMD5AndDES", "Hex");
			stringTest = quotaID & answer;
			
			if ( stringOrignal eq stringTest ) {
				postSecure = true;
			}
		}
		catch(any excpt) {
		}
		
		if ( postSecure ) {
				
			transaction {
				
				sql = "	SELECT
							q.quo_id,q.qsn_id
						FROM
							t_quota q
						WHERE
							q.quo_id = :quotaId ";
				
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name="quotaId", value=quotaId, cfsqltype="cf_sql_char");
				rs_quota = queryObj.execute(sql=sql).getResult();
				
				sequenceQuestionVote.setStudentID(studentId);
				sequenceQuestionVote.setQuotaID(quotaId);
				voteId = sequenceQuestionVote.getID();
				
				sql = "	SELECT
							v.vote_id
						FROM
							t_vote_response v
						WHERE
							v.vote_id = :voteId ";
				
				queryObj = new Query(datasource = application.dnsMaster);

				queryObj.addParam(name="voteId", value=voteId, cfsqltype="cf_sql_varchar");
				
				rs_vote = queryObj.execute(sql=sql).getResult();
				
				
				/* 添加更新投票记录 */
				if( rs_vote.recordCount ){
					
					sql = "UPDATE t_vote_response v
							 SET
								v.stu_id = :studentId ,
								v.quo_id = :quotaId ,
								v.answer = :answer 
							 WHERE
								v.vote_id = :voteId ";
					updateObj = new Query( datasource = application.dnsMaster);
					
					updateObj.addParam(name="voteId", value=voteId, cfsqltype="cf_sql_varchar");
					updateObj.addParam(name="studentId", value=studentId, cfsqltype="cf_sql_varchar");
					updateObj.addParam(name="answer", value=answer, cfsqltype="cf_sql_varchar");
					updateObj.addParam(name="quotaId", value=quotaId, cfsqltype="cf_sql_char");
					
					updateObj.execute(sql=sql);
					
				}else{
					
					sql = "INSERT INTO t_vote_response (
								vote_id, 
								stu_id, 
								quo_id, 
								answer
							) VALUES (
								:voteId ,
								:studentId ,
								:quotaId ,
								:answer 
							)";
					
					updateObj = new Query(datasource = application.dnsMaster);
					
					updateObj.addParam(name="voteId", value=voteId, cfsqltype="cf_sql_varchar");
					updateObj.addParam(name="studentId", value=studentId, cfsqltype="cf_sql_varchar");
					updateObj.addParam(name="answer", value=answer, cfsqltype="cf_sql_varchar");
					updateObj.addParam(name="quotaId", value=quotaId, cfsqltype="cf_sql_char");
					
					updateObj.execute(sql=sql);
					
				}

			}
			
			/*查询数量*/
			sql = "	SELECT
						count(*) r_count
					FROM
						t_vote_response v
						INNER JOIN t_quota q ON q.quo_id = v.quo_id
					WHERE
							q.qsn_id = :sheetId 
						AND
							v.stu_id = :studentId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
					
			queryObj.addParam(name="sheetId", value=rs_quota.qsn_id, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name="studentId", value=studentId, cfsqltype="cf_sql_varchar");

			rs_count = queryObj.execute(sql=sql).getResult();
			
			structInsert(response, "success", true, true);
			structInsert(response, "optDone", rs_count.r_count, true);
			
		}
		else {
			structInsert(response, "reason", "参数错误", true);
		}

	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>